package com.kedi.usercenter.applogin.controller;

import com.google.common.collect.Maps;
import com.kedi.usercenter.config.UserConfig;
import com.kedi.usercenter.service.LoginService;
import com.kedi.usercenter.service.UserRegisterService;
import com.kedi.usercenter.utils.JwtUtils;
import com.kedi.usercenter.utils.ResultVoUtil;
import com.kedi.usercenter.vo.ResultVo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;

/**
 * @Description:    登录控制层
 * @Author:         ytw
 * @CreateDate:     2019/7/22 16:52
 * @UpdateRemark:   修改内容
 * @Version:        1.0
 */
@Controller
public class LoginController {
    private static Logger logger = LoggerFactory.getLogger(LoginController.class);
    @Autowired
    private LoginService service;
    @Autowired
    private UserRegisterService registService;
    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    UserConfig config;

    /**
     * 方法实现说明  根据手机和密码进行登录
     * @author      ytw
     *@param         password,phone
     * @return
     * @exception
     * @date        2019/7/16 17:18
     */
    @ResponseBody
    @PostMapping("/user/loginByPassword")
    ResultVo loginByPhoneAndPs(@RequestParam("password") String password, @RequestParam("phone") String phone){
        String mPassword = DigestUtils.md5DigestAsHex(password.getBytes());
        Map<String, Object> res = service.selectByNameAndPs(mPassword, phone);
        String useraddress = registService.selectpuseraddress(res);
        res.put("useraddress",useraddress);
        res.put("code",phone);
        if(res==null){
            logger.info("输入电话或密码错误");
            return ResultVoUtil.error(0,"输入电话或密码错误");
        }else {
            return ResultVoUtil.success(res);
        }
    }

    /**
     * 方法实现说明  根据手机和验证码进行登录
     * @author      ytw
     *@param         phone,securitycode
     * @return
     * @exception
     * @date        2019/7/16 17:18
     */
    @ResponseBody
    @PostMapping("/user/loginByPhoneAndCode")
    ResultVo loginByPhoneAndCode(@RequestParam("phone") String phone, @RequestParam("securitycode")String securitycode){
        String redisCode = redisTemplate.opsForValue().get(config.getkeyPrefix() + phone);
        if(!StringUtils.equals(redisCode,securitycode)){
            return ResultVoUtil.error(0,"验证码不匹配");
        }
        Map<String, Object> res = service.selectByPhoneAndcode(phone);
        String useraddress = registService.selectpuseraddress(res);
        res.put("useraddress",useraddress);
        res.put("code",phone);
        if(res==null){
            return ResultVoUtil.error(0,"用户尚未注册");
        }else {
            return ResultVoUtil.success(res);
        }

    }
    /**
     * 方法实现说明  登录成功生成token 回调成功的路径
     * @author      ytw
     *@param         code=1表示登录成功,id,name,idcard,headImg,state跳转的路径,response
     * @return
     * @exception   IOException
     * @date        2019/7/30 1:22
     */
    @GetMapping("/user/callback")
    public void UserCallback(@RequestParam("code")String code,@RequestParam("id")Integer id, @RequestParam("name")String name,@RequestParam("idcard")String idcard
            , @RequestParam(value="headImg",required = false)String headImg,
                             String state, HttpServletResponse response) throws IOException {
        if(!StringUtils.equals(code,"1")){
            return ;
        }
        Map<String, Object> map = Maps.newHashMapWithExpectedSize(10);
        map.put("id",id);
        map.put("name",name);
        map.put("idcard",idcard);
        String token = JwtUtils.geneJsonWebToken(map);

        response.sendRedirect(state+"?token="+token+"&head_img="+headImg+"&name="+URLEncoder.encode(name,"UTF-8")
                +"&id="+id);

    }


}
